package com.xinxing.city.recovery.ui;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import com.xinxing.city.recovery.Constant;
import com.xinxing.city.recovery.MainActivity800;
import com.xinxing.city.recovery.R;
import com.xinxing.city.recovery.Tools;
import com.xinxing.city.recovery.databinding.ActivitySetBaseBinding;
import com.xinxing.city.recovery.entitys.SetBase;
import com.xinxing.city.recovery.entitys.SetReceive;
import com.xinxing.city.recovery.entitys.UpdataApk;
import com.xinxing.city.recovery.interfaces.DownloadListener;
import com.xinxing.city.recovery.net.BaseTask;
import com.xinxing.city.recovery.net.RetroFactory;
import com.xinxing.city.recovery.utils.CheckUpdate;
import com.xinxing.city.recovery.utils.MyDialog;
import com.xinxing.city.recovery.utils.SharedPreferencesUtils;
import com.xinxing.city.recovery.utils.VersionUtil;

import java.lang.reflect.Type;

public class SetBaseActivity extends AppCompatActivity {
    private ActivitySetBaseBinding binding;
    private SetBase setBase = new SetBase();
    private MyDialog myDialog1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_set_base);
        PackageManager manager = getPackageManager();
        PackageInfo info = null;
        try {
            info = manager.getPackageInfo(getPackageName(), 0);
            String versionName = info.versionName;
//            int versionCode = info.versionCode;
            setBase.setVersion(versionName);

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        init();
        String currentVersionCode = info.versionName;
        binding.setSetBase(setBase);
        binding.llyVersion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //版本更新
                updata(currentVersionCode);
            }
        });
    }
    private void updata(String version) {
        new BaseTask<UpdataApk>(this, RetroFactory.getJsonService().getUpdataApk("recycle-cabinet", "android", version)).handleResponse(new BaseTask.ResponseListener<UpdataApk>() {


            @Override
            public void onSuccess(UpdataApk updataApk) {
                VersionUtil versionUtils = new VersionUtil();
                int v = versionUtils.compareVersion(version, updataApk.getVersion());
                Log.e("TTT", "v " + v + "   version1 " + version + "  version2 " + updataApk.getVersion());
                if (v < 0) {
                    myDialog1 = MyDialog.newInstance("提示","正在下载","","");
                    myDialog1.show(getSupportFragmentManager(),"MyDialog");

                    String parentFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download";
                    CheckUpdate checkUpdate = new CheckUpdate(SetBaseActivity.this);
                    checkUpdate.setDownloadListener(new DownloadListener() {
                        @Override
                        public void pending(int totalBytes) {

                        }

                        @Override
                        public void progress(int soFarBytes, int totalBytes) {

                        }

                        @Override
                        public void completed() {
                            myDialog1.dismiss();
                        }

                        @Override
                        public void error() {
                            myDialog1.dismiss();
                        }
                    });
                    String url = Constant.BaseUrl + "/sr-asset/v1/assets?a=d&p=" + updataApk.getPkgPath();
                    checkUpdate.downNewApk(url, parentFile, "b.apk", CheckUpdate.DisplayMode.MODE_DIALOG_BOX);
                }else{
                    Toast.makeText(SetBaseActivity.this,"已是最新版本",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail() {

            }
        });
    }
    private void init(){
        String str = (String) SharedPreferencesUtils.getParam(this,"ops_base","");
        if(!str.equals("")){
            Type dev = new TypeToken<SetBase>() {}.getType();
            Gson gson = new Gson();;
            setBase = gson.fromJson(str,dev);
        }
    }
    public void saveSub(View view) {
        Gson gson = new Gson();
        SharedPreferencesUtils.setParam(this, "ops_base", gson.toJson(setBase));
        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
        this.finish();
    }
}
